package com.haha;

import com.sun.jna.platform.win32.User32;
import com.sun.jna.platform.win32.WinDef;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.util.List;

public class DruidActionListener implements ActionListener {
    /**
     * 获取魔兽世界的窗口
     */
    private final WinDef.RECT wowWindow = getWorldOfWarcraftWindow();
    private final List<Integer> centerList;
    private final Color red = new Color(255, 0, 0);
    private final Color green = new Color(0, 255, 0);
    int topAdd = 3;
    private Robot robot;

    public DruidActionListener(List<Integer> centerList) {
        this.centerList = centerList;
        try {
            robot = new Robot();
        } catch (AWTException awtException) {
            awtException.printStackTrace();
            System.out.println("初始化失败");
        }
    }

    /**
     * -- 从左到右，颜色对应的含义
     * -- 1-1 单体开关，绿色为开,红色为关
     * -- 1-2 AOE开关，绿色为开，红色为关，预留位置，暂时没有
     * -- 1-3 裂伤（豹），绿色为需要打，红色为不需要打
     * -- 1-4 野蛮咆哮，绿色为需要打，红色为不需要打
     * -- 1-5 斜掠，绿色为需要打，红色为不需要打
     * -- 1-6 撕碎，绿色为需要打，红色为不需要打
     * -- 1-7 割裂，绿色为需要打，红色为不需要打
     * -- 1-8
     * -- 1-9
     *
     * @param e e
     */
    @Override
    public void actionPerformed(ActionEvent e) {

        // 获取单目标的开关
        int winLeft = wowWindow.left + centerList.get(0);
        int winTop = wowWindow.top + topAdd;
        Color oneDamageSwitchColor = robot.getPixelColor(winLeft, winTop);

        // 多目标的开关
        winLeft = wowWindow.left + centerList.get(1);
        winTop = wowWindow.top + topAdd;
        Color moreTargetSwitchColor = robot.getPixelColor(winLeft, winTop);

        if (green.equals(oneDamageSwitchColor)) {
            oneTarget();
        } else if (green.equals(moreTargetSwitchColor)) {
            moreTarget();
        }
    }

    private void pressKey(String key) {
        try {
            int keyCode = KeyEvent.class.getDeclaredField("VK_" + key).getInt(null);
            robot.keyPress(keyCode);
            robot.keyRelease(keyCode);
        } catch (Exception ex) {
            System.err.println("无效按键配置: " + key);
        }
    }

    public WinDef.RECT getWorldOfWarcraftWindow() {
        WinDef.HWND hwnd = User32.INSTANCE.FindWindow(null, "魔兽世界");
        WinDef.RECT winRect = new WinDef.RECT();
        User32.INSTANCE.GetWindowRect(hwnd, winRect);
        return winRect;
    }

    private void oneTarget() {
        // 裂伤（豹）
        int winLeft = wowWindow.left + centerList.get(2);
        int winTop = wowWindow.top + topAdd;
        Color lsColor = robot.getPixelColor(winLeft, winTop);
        if (green.equals(lsColor)) {
            // 使用配置的按键
//            String savageRoarKey = ConfigManager.getKeyBind("德鲁伊-野德-WLK","裂伤（豹）");
//            pressKey(savageRoarKey);
            robot.keyPress(KeyEvent.VK_EQUALS);
            robot.keyRelease(KeyEvent.VK_EQUALS);
            return;
        }

        winLeft = wowWindow.left + centerList.get(3);
        winTop = wowWindow.top + topAdd;
        Color ympxColor = robot.getPixelColor(winLeft, winTop);
        if (green.equals(ympxColor)) {
            // 使用配置的按键
//            String savageRoarKey = ConfigManager.getKeyBind("德鲁伊-野德-WLK","野蛮咆哮");
//            pressKey(savageRoarKey);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_I);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_I);
            return;
        }

        winLeft = wowWindow.left + centerList.get(4);
        winTop = wowWindow.top + topAdd;
        Color xlColor = robot.getPixelColor(winLeft, winTop);
        if (green.equals(xlColor)) {
//            String savageRoarKey = ConfigManager.getKeyBind("德鲁伊-野德-WLK","斜掠");
//            pressKey(savageRoarKey);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_O);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_O);
            return;
        }

        winLeft = wowWindow.left + centerList.get(6);
        winTop = wowWindow.top + topAdd;
        Color glColor = robot.getPixelColor(winLeft, winTop);
        if (green.equals(glColor)) {
//            String savageRoarKey = ConfigManager.getKeyBind("德鲁伊-野德-WLK","割裂");
//            pressKey(savageRoarKey);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_K);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_K);
            return;
        }

        winLeft = wowWindow.left + centerList.get(7);
        winTop = wowWindow.top + topAdd;
        Color jlzhColor = robot.getPixelColor(winLeft, winTop);
        if (green.equals(jlzhColor)) {
//            String savageRoarKey = ConfigManager.getKeyBind("德鲁伊-野德-WLK","精灵之火（野性）");
//            pressKey(savageRoarKey);
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_J);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_J);
            return;
        }

        // 默认打撕碎
//        String savageRoarKey = ConfigManager.getKeyBind("德鲁伊-野德-WLK","撕碎");
//        pressKey(savageRoarKey);
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_L);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_L);

    }

    private void moreTarget() {
        // 暂时停止输出
        int winLeft = wowWindow.left + centerList.get(7);
        int winTop = wowWindow.top + topAdd;
        Color allSwitchColor = robot.getPixelColor(winLeft, winTop);
        if (red.equals(allSwitchColor)) {
            System.out.println("开关关闭暂停输出");
            return;
        }

        // 杀戮射击
        winLeft = wowWindow.left + centerList.get(8);
        winTop = wowWindow.top + topAdd;
        Color slsjColor = robot.getPixelColor(winLeft, winTop);
        if (green.equals(slsjColor)) {
            robot.keyPress(KeyEvent.VK_0);
            robot.keyRelease(KeyEvent.VK_0);
            return;
        }

        // 4级爆炸射击
        winLeft = wowWindow.left + centerList.get(1);
        winTop = wowWindow.top + topAdd;
        Color bzsj4jColor = robot.getPixelColor(winLeft, winTop);
        if (green.equals(bzsj4jColor)) {
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_ALT);
            robot.keyPress(KeyEvent.VK_N);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_ALT);
            robot.keyRelease(KeyEvent.VK_N);
            return;
        }

        // 3级爆炸射击
        winLeft = wowWindow.left + centerList.get(2);
        winTop = wowWindow.top + topAdd;
        Color bzsj3jColor = robot.getPixelColor(winLeft, winTop);
        if (green.equals(bzsj3jColor)) {
            robot.keyPress(KeyEvent.VK_9);
            robot.keyRelease(KeyEvent.VK_9);
            return;
        }

        // 取毒蛇钉刺的颜色
        winLeft = wowWindow.left + centerList.get(0);
        winTop = wowWindow.top + topAdd;
        Color dsdcColor = robot.getPixelColor(winLeft, winTop);
        if (red.equals(dsdcColor)) {
            robot.keyPress(KeyEvent.VK_CONTROL);
            robot.keyPress(KeyEvent.VK_ALT);
            robot.keyPress(KeyEvent.VK_U);
            robot.keyRelease(KeyEvent.VK_CONTROL);
            robot.keyRelease(KeyEvent.VK_ALT);
            robot.keyRelease(KeyEvent.VK_U);
            return;
        }

        // 多重射击
        winLeft = wowWindow.left + centerList.get(6);
        winTop = wowWindow.top + topAdd;
        Color mzsjColor = robot.getPixelColor(winLeft, winTop);
        if (green.equals(mzsjColor)) {
            robot.keyPress(KeyEvent.VK_EQUALS);
            robot.keyRelease(KeyEvent.VK_EQUALS);
            return;
        }

        // 默认打稳固射击
        robot.keyPress(KeyEvent.VK_CONTROL);
        robot.keyPress(KeyEvent.VK_ALT);
        robot.keyPress(KeyEvent.VK_I);
        robot.keyRelease(KeyEvent.VK_CONTROL);
        robot.keyRelease(KeyEvent.VK_ALT);
        robot.keyRelease(KeyEvent.VK_I);
    }
}
